/*
  *****************************************提示框组件*********************************************
 1. tmsgbox.alert({message:"my message", type:"warning", title:"my title"});
 2. tmsgbox.confirm({message:"my message", type:"error"}).on(function (e) {});
 3. tmsgbox.dialog({title:"my title",url:"<%=request.getContextPath()%>/frontend/test"});
*/
function JyyMsgBox() {
}
JyyMsgBox.init = function(options, isDialog) {
	this.options = {
		title: "操作提示",
		message: "提示内容",
		btnok: "确定",
		btncl: "取消",
		auto: false,
		type: "glyphicon-info-sign tmsgbox-icon-info"
	};
	this.msghtml = '<div class="modal fade bs-example-modal-sm tmsgbox-bg" id="[Id]" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
          '	<div class="modal-dialog modal-sm" role="document">' +
          '		<div class="modal-content tmsgbox-content">' +
          '			<div class="modal-header tmsgbox-h">' +
          '				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
          '				<h4 class="modal-title tmsgbox-title" id="myModalLabel">[Title]</h4>' +
          '			</div>' +
          '			<div class="modal-body row tmsgbox-body">' +
          '				<div class="col-xs-3 tmsgbox-pos1"><span class="glyphicon [Type] tmsgbox-icon"></span></div>' +
          '				<div class="col-xs-9 tmsgbox-pos2"><span>[Message]</span></div>' +
          '			</div>' +
          '			<div class="modal-footer tmsgbox-footer">' +
          '				<button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>' +
          '				<button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>' +
          '			</div>' +
          '		</div>' +
          '	</div>' +
          '</div>';
	this.dialoghtml =  '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">' +
						'	<div class="modal-dialog">' +
						'		<div class="modal-content">' +
             '			<div class="modal-header tmsgbox-dialog-h">' +
            '				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' +
            '				<h4 class="modal-title" id="modalLabel">[Title]</h4>' +
            '			</div>' +
            '			<div class="modal-body"></div>' +
            '		</div>' +
            '	</div>' +
         	    '</div>';
	this.reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');
	$.extend(this.options, options);
	
	if(isDialog) return;
	
	var msgId = "jyymsg" + JyyCommon.getUUID();
	var content = this.msghtml.replace(this.reg, function (node, key) {
		return {
			Id: msgId,
			Title: JyyMsgBox.options.title,
			Message: JyyMsgBox.options.message,
			BtnOk: JyyMsgBox.options.btnok,
			BtnCancel: JyyMsgBox.options.btncl,
			Type: JyyMsgBox.options.type
		}[key];
	});
	$('body').append(content);
	$('#' + msgId).modal({
		backdrop:false,
		keyboard: true
	});
	// 居中设置
	var modalHeight = $(window).height() / 2 - $('#' + msgId + ' .modal-dialog').height() / 2;
	$('#' + msgId).find('.modal-dialog').css({'margin-top': modalHeight});
	            
	$('#' + msgId).on('hide.bs.modal', function (e) {
		$('body').find('#' + msgId).remove();
	});
	return msgId;
}

JyyMsgBox.alert = function(options) {
	if (typeof options == 'string') {
		options = {message: options};
	} else if(typeof options == 'object') {
		if (options.type == "warning") {
			options.type = "glyphicon-exclamation-sign tmsgbox-icon-warning";
		} else if (options.type == "error") {
			options.type = "glyphicon-remove-sign tmsgbox-icon-error";
		} else {
			options.type = "glyphicon-info-sign tmsgbox-icon-info";
		}
	}
	var id = this.init(options);
	var msg = $('#' + id);
	msg.find('.cancel').hide();
	
	return {
		id: id,
		on: function (callback) {
			if (callback && callback instanceof Function) {
				msg.find('.ok').click(function () { callback(true); });
			}
		},
		hide: function (callback) {
			if (callback && callback instanceof Function) {
				msg.on('hide.bs.modal', function (e) {callback(e);});
			}
		}
	};
}

JyyMsgBox.confirm = function(options) {
	if (typeof options == 'string') {
		options = {message: options};
	} else if(typeof options == 'object') {
		if (options.type == "warning") {
			options.type = "glyphicon-exclamation-sign tmsgbox-icon-warning";
		} else if (options.type == "error") {
			options.type = "glyphicon-remove-sign tmsgbox-icon-error";
		} else {
			options.type = "glyphicon-info-sign tmsgbox-icon-info";
		}
	}
	var id = this.init(options);
	var msg = $('#' + id);
	msg.find('.cancel').show();

	return {
		id: id,
		on: function (callback) {
			if (callback && callback instanceof Function) {
				msg.find('.ok').click(function () { callback(true); });
				msg.find('.cancel').click(function () { callback(false); });
			}
		},
		hide: function (callback) {
			if (callback && callback instanceof Function) {
				msg.on('hide.bs.modal', function (e) {
					callback(e);
				});
			}
		}
	};
}

JyyMsgBox.dialog = function(options) {

	options.onReady = function (e){};
	options.onShown = function (e){};
	this.init(options, true);
	
	var msgId = "jyydialog" + JyyCommon.getUUID();

	var content = this.dialoghtml.replace(this.reg, function (node, key) {
		return {
			Id: msgId,
			Title: JyyMsgBox.options.title
		}[key];
	});

	$('body').append(content);
	var target = $('#' + msgId);
	target.find('.modal-body').load(this.options.url);
	
	if (this.options.onReady()) this.options.onReady.call(target);
	target.modal({backdrop:false,keyboard: true});
                
	// 设置宽度
	if (this.options.width != 'undefined') {
		target.find('.modal-dialog').css({'width': this.options.width});
	}

	target.on('shown.bs.modal', function (e) {
		if (JyyMsgBox.options.onReady(e)) JyyMsgBox.options.onReady.call(target, e);
	});
	target.on('hide.bs.modal', function (e) {
		$('body').find(target).remove();
	});
};